package com.ag.hmk.action;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.ag.bean.TbLaw;
import com.ag.bean.TbLkw;
import com.ag.bean.TbUri;
import com.ag.common.AnnexPath;
import com.ag.common.BaseAction;
import com.ag.common.IDUtil;
import com.ag.hmk.service.impl.HmkUiltServImpl;
import com.ag.hmk.service.impl.WordToHtmlSerImpl;
import com.ag.lzm.dao.TbUriDAO;

/**
 * 
 * @author hmk
 * 查询某一法规信息
 */
public class FindOneLawAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private HmkUiltServImpl hmkUiltServImpl;
	private WordToHtmlSerImpl wordToHtmlSerImpl;
	@Resource(name = "LZMTbUriDAO")
	private TbUriDAO uriDAO;

	private String lawID1;  //法规ID
	private String findType1; //查询类别（导向到不同的页面）

	private TbLaw lawRst;
	private String lawKeyword;
	private String htmlName;  //附件的HTML文档名

	/*
	 * 查询一个法律
	 */
	public String findOneLaw() throws Exception {
		TbLaw law = null;
		String keyword = "";
		System.out.println("---findOneLaw");
		try {
			String hql = "from TbLaw l left join fetch l.tbType left join fetch l.tbLawnature left join fetch l.tbLkws k  where 1=1";
			System.out.println("------" + lawID1);
			if (lawID1 != null && lawID1.length() > 0) {
				hql += " and l.a='" + IDUtil.PRESERVE + "' and l.lawId = " + lawID1;
				List result = null;
				result = hmkUiltServImpl.hqlQuery(hql);
				if (result != null) {
					law = (TbLaw) result.iterator().next();
					Set<TbLkw> lkwSet = null;
					lkwSet = (Set<TbLkw>) law.getTbLkws();
					Iterator<TbLkw> it2 = lkwSet.iterator();
					while (it2.hasNext()) {
						keyword += (it2.next().getLawKw() + ",");
					}
				}

			} else {
				System.out.println("-----lawID1 is null");
			}
		} catch (Exception e) {
			e.printStackTrace();
			law = null;
			keyword = "";
		}

		this.setLawRst(law);
		this.setLawKeyword(keyword);

		if (findType1 != null && findType1.equals("1")) {
			System.out.println("-----type:" + findType1);
			//返回法规编辑
			return "goLawEidtOK";
		} else if (findType1 != null && findType1.equals("2")) {
			// 进行DOC文档到HTML文档的转换
			if (law != null) {
				try {
					String myname = law.getLawFjName();
					String name = myname.substring(0, myname.lastIndexOf(".")); 
					System.out.println("nanm:" + name);
					String Objpath1 = getServletContext().getRealPath("/") + "hmkuploads/";
					String Objpath = Objpath1 + name + ".html";
					String srcPath = "";
					TbUri uri1 = uriDAO.findById(AnnexPath.LAW_ANNEX_PATH);
					if (uri1 != null) {
						srcPath = uri1.getUPath() + law.getLawFjName();
					} else {
						srcPath = Objpath1 + law.getLawFjName();
					}

					// 判断文件是否存在
					if (wordToHtmlSerImpl.isFileExist(Objpath)) {
						this.htmlName = name + ".html";
					} else {
						// 进行文件转换并复制到工程目录相应的文件夹中
						if (wordToHtmlSerImpl.wordToHtml(srcPath, Objpath)) {
							System.out.println("===ok");
							this.htmlName = name + ".html";
						} else {
							this.htmlName = "";
						}
					}
					//
				} catch (Exception e) {
					e.printStackTrace();
					this.htmlName = "";
				}
			}
			//返回到事件关联的法规详情
			return "goeventLawDetail";
			
		} else {
			System.out.println("-------vv -type:" + findType1);
			if (law != null) {
				try {
					// 进行DOC文档到HTML文档的转换
					String myname = law.getLawFjName();
					String name = myname.substring(0, myname.lastIndexOf(".")); // UUID.randomUUID().toString();
																				// //law.getLawFjName();
					System.out.println("nanm:" + name);
					String Objpath2 = getServletContext().getRealPath("/");
					Objpath2 += "hmkuploads/" + name + ".html";
					String srcPath2 = "";
					TbUri uri2 = uriDAO.findById(AnnexPath.LAW_ANNEX_PATH);
					if (uri2 != null) {
						srcPath2 = uri2.getUPath() + law.getLawFjName();
					} else {
						srcPath2 = Objpath2 + law.getLawFjName();
					}
					// 判断文件是否存在
					if (wordToHtmlSerImpl.isFileExist(Objpath2)) {
						this.htmlName = name + ".html";
					} else {
						// 进行文件转换并复制到工程目录相应的文件夹中
						if (wordToHtmlSerImpl.wordToHtml(srcPath2, Objpath2)) {
							System.out.println("===ok");
							this.htmlName = name + ".html";
						} else {
							this.htmlName = "";
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
					this.htmlName = "";
				}
			}
			//返回法规详情
			return "findOneLawOK";
		}
		
	}

	public void setHmkUiltServImpl(HmkUiltServImpl hmkUiltServImpl) {
		this.hmkUiltServImpl = hmkUiltServImpl;
	}

	public TbLaw getLawRst() {
		return lawRst;
	}

	public void setLawRst(TbLaw lawRst) {
		this.lawRst = lawRst;
	}

	public void setLawID1(String lawID1) {
		this.lawID1 = lawID1;
	}

	public String getLawKeyword() {
		return lawKeyword;
	}

	public void setLawKeyword(String lawKeyword) {
		this.lawKeyword = lawKeyword;
	}

	public void setFindType1(String findType1) {
		this.findType1 = findType1;
	}

	public void setWordToHtmlSerImpl(WordToHtmlSerImpl wordToHtmlSerImpl) {
		this.wordToHtmlSerImpl = wordToHtmlSerImpl;
	}

	public String getHtmlName() {
		return htmlName;
	}

	public void setHtmlName(String htmlName) {
		this.htmlName = htmlName;
	}
}
